package com.ruiben.mall.impl;

import com.ruiben.mall.ISearchService;
import com.ruiben.mall.mapper.ShopProductMapper;
import com.ruiben.mall.pojo.ShopProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private ShopProductMapper productMapper;

    @Override
    public List<ShopProduct> searchGoods(String name,int sort) {
        Example example = new Example(ShopProduct.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isdelete",0);
        criteria.andLike("productname", "%"+name+"%");

        if (sort == 1){
            example.setOrderByClause("price asc");
        } else {
            example.setOrderByClause("price desc");
        }
        return productMapper.selectByExample(example);
    }
}
